package com.qianchang.microfinance.view.credit.data;

import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.bigkoo.pickerview.model.IPickerViewData;
import com.dm.library.http.DMOKHttpUtils;
import com.dm.library.http.HttpParams;
import com.dm.library.http.base.JsonResult;
import com.dm.library.http.callback.RequestCallBack;
import com.dm.library.utils.ToastUtil;
import com.dm.library.widgets.OneLineEditLinearLayout;
import com.dm.library.widgets.OneLineLinearLayout;
import com.qianchang.microfinance.R;
import com.qianchang.microfinance.bean.BasicOwnerBean;
import com.qianchang.microfinance.bean.CommonSinglePickBean;
import com.qianchang.microfinance.bean.LoanType;
import com.qianchang.microfinance.global.ConstantAppInfo;
import com.qianchang.microfinance.utils.DMConstant;
import com.qianchang.microfinance.view.base.BaseActivity;
import com.qianchang.microfinance.view.dialog.AreaDialog;
import com.qianchang.microfinance.view.dialog.CommonDialog;

import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.qianchang.microfinance.R.id.oll_house_area;

/**
 * 业主贷 基本信息
 */


public class BasicOwnerActivity extends BaseActivity implements OneLineEditLinearLayout.TextWatcher {

    private static final String TAG = "BasicStudentActivity";
    @BindView(R.id.oll_building_name)
    OneLineEditLinearLayout ollBuildingName;
    @BindView(R.id.oll_area)
    OneLineLinearLayout ollArea;
    @BindView(R.id.oll_address_detail)
    OneLineEditLinearLayout ollAddressDetail;
    @BindView(R.id.oll_building_no)
    OneLineEditLinearLayout ollBuildingNo;
    @BindView(R.id.oll_room_no)
    OneLineEditLinearLayout ollRoomNo;
    @BindView(oll_house_area)
    OneLineEditLinearLayout ollHouseArea;
    @BindView(R.id.oll_house_use)
    OneLineLinearLayout ollHouseUse;
    @BindView(R.id.oll_developers)
    OneLineEditLinearLayout ollDevelopers;
    @BindView(R.id.ll_content)
    LinearLayout llContent;
    @BindView(R.id.btn_commit)
    Button btnCommit;

    private AreaDialog areaDialog;
    private String mProvinceId;
    private String mCityId;
    private String mAreaId;

    private String developerName; //" : "万科", //开发商
    private String area;         //" : "100", //面积
    private String estateAdress; //" : "民治路395号",
    private String estateArea;   //    " : "深圳市龙岗区", //楼盘区域
    private String estateName;   //    " : "四季花城", // 楼盘名称
    private String houseNum;     //" : "8", //栋号
    private String roomNum;      //" : "81001", //房间号
    private String usage;        //" : "租赁" //用途
    private String currentId;

    private String projectType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_basic_owner);//
        ButterKnife.bind(this);
        initView();
        initData();
    }

    @Override
    protected void initView() {
        super.initView();
        ollBuildingName.setTextWatcher(this, 1);
        ollAddressDetail.setTextWatcher(this, 2);
        ollBuildingNo.setTextWatcher(this, 3);
        ollRoomNo.setTextWatcher(this, 4);
        ollHouseArea.setTextWatcher(this, 5);
        ollDevelopers.setTextWatcher(this, 6);

        ollBuildingName.setMaxLength(12);
        ollAddressDetail.setMaxLength(12);
        ollBuildingNo.setMaxLength(12);
        ollRoomNo.setMaxLength(12);
        ollDevelopers.setMaxLength(20);
        ollHouseArea.setMaxLength(12);
        ollHouseArea.setInputType(InputType.TYPE_CLASS_NUMBER);
    }


    @Override
    protected void initData() {
        super.initData();
        Bundle bundle = getBundle();
        if (null != bundle) {
            currentId = bundle.getString(ConfirmInfoJinbaoActivity.CURRENT_ID);
            projectType = bundle.getString(ConstantAppInfo.PROJECT_TYPE);
        }
        getBasicInfo();
    }

    private void getBasicInfo() {
        show();
        HttpParams params = new HttpParams();
        params.put("projectId", currentId);
        DMOKHttpUtils.getInstance().post(this, DMConstant.Config.BASE_USER + "/project/" + LoanType.getNameByType(projectType) + DMConstant.Config.V + "/getBaseInfo.do", params, new RequestCallBack<BasicOwnerBean>() {
            @Override
            public void onSuccess(JsonResult<BasicOwnerBean> response) {
                dismiss();
                if (DMConstant.Code.CODE_OK.equals(response.getCode())) {
                    BasicOwnerBean basicOwnerBean = response.getObject();
                    if (null != basicOwnerBean) {
                        refreshUi(basicOwnerBean);
                    }
                } else {
                    ToastUtil.getInstant().show(BasicOwnerActivity.this, response.getDescription());
                }
            }

            @Override
            public void onFailure(String msg) {
                dismiss();
                ToastUtil.getInstant().show(BasicOwnerActivity.this, msg);
            }

            @Override
            public void onConnectFailure(String msg) {
                ToastUtil.getInstant().show(BasicOwnerActivity.this, msg);
            }
        });
    }

    private void refreshUi(BasicOwnerBean basicOwnerBean) {
//        btnCommit.setVisibility(View.GONE);
//        ViewUtils.disableSubControls(llContent);
        estateArea = basicOwnerBean.getEstateArea();
        usage = basicOwnerBean.getUsage();
        estateName = basicOwnerBean.getEstateName();
        estateAdress = basicOwnerBean.getEstateAdress();
        houseNum = basicOwnerBean.getHouseNum();
        roomNum = basicOwnerBean.getRoomNum();
        area = basicOwnerBean.getArea();
        developerName = basicOwnerBean.getDeveloperName();

        ollArea.setRightContent(estateArea);
        ollHouseUse.setRightContent(usage);
        ollBuildingName.setRightContent(estateName);
        ollAddressDetail.setRightContent(estateAdress);
        ollBuildingNo.setRightContent(houseNum);
        ollRoomNo.setRightContent(roomNum);
        ollHouseArea.setRightContent(area);
        ollDevelopers.setRightContent(developerName);
    }

    private void submit() {
        if (TextUtils.isEmpty(developerName)
                || TextUtils.isEmpty(area)
                || TextUtils.isEmpty(estateAdress)
                || TextUtils.isEmpty(estateArea)
                || TextUtils.isEmpty(estateName)
                || TextUtils.isEmpty(houseNum)
                || TextUtils.isEmpty(roomNum)
                || TextUtils.isEmpty(usage)
                ) {
            ToastUtil.getInstant().show(BasicOwnerActivity.this, getString(R.string.msg_finish_basic_info));
            return;
        }
        show();

        HttpParams params = new HttpParams();
        params.put("provinceId", mProvinceId);
        params.put("cityId", mCityId);
        params.put("countryId", mAreaId);
        params.put("projectId", currentId);

        params.put("estateName", estateName);
        params.put("estateAdress", estateAdress);
        params.put("estateArea", estateArea);
        params.put("houseNum", houseNum);
        params.put("roomNum", roomNum);
        params.put("area", area);
        params.put("usage", usage);
        params.put("developerName", developerName);

        DMOKHttpUtils.getInstance().post(this, DMConstant.Config.BASE_USER + "/project/" + LoanType.getNameByType(projectType) + DMConstant.Config.V + "/insertBaseInfo.do", params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(JsonResult<String> response) {
                dismiss();
                if (response.isReqDataSuccess()) {
                    setResult(RESULT_OK);
                    BasicOwnerActivity.this.finish();
                } else {
                    ToastUtil.getInstant().show(BasicOwnerActivity.this, response.getDescription());
                }
            }

            @Override
            public void onFailure(String msg) {
                dismiss();
                ToastUtil.getInstant().show(BasicOwnerActivity.this, msg);
            }

            @Override
            public void onConnectFailure(String msg) {
                ToastUtil.getInstant().show(BasicOwnerActivity.this, msg);
            }

        });
    }

    private String getFormatTime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        return format.format(date);
    }

    @OnClick({R.id.oll_area, R.id.oll_house_use, R.id.btn_commit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.oll_area:
                if (areaDialog == null) {
                    areaDialog = new AreaDialog(this);
                    areaDialog.setOnAreaSelectListener(new AreaDialog.OnAreaSelectListener() {
                        @Override
                        public void onAreaSelect(String proviceId, String cityId, String areaId, String result) {
                            BasicOwnerActivity.this.mProvinceId = proviceId;
                            BasicOwnerActivity.this.mCityId = cityId;
                            BasicOwnerActivity.this.mAreaId = areaId;
                            estateArea = result;
                            ollArea.setRightContent(result);
                        }
                    });
                }
                areaDialog.showPickerView();
                break;
            case R.id.oll_house_use:
                CommonDialog.Builder builder = CommonDialog.init()
                        .setOnItemSelectListener(new CommonDialog.OnItemSelectListener() {
                            @Override
                            public void onItemSelect(IPickerViewData iPickerViewData, int position, int flag) {
                                CommonSinglePickBean commonSinglePickBean = (CommonSinglePickBean) iPickerViewData;
                                usage = commonSinglePickBean.getName();
                                ollHouseUse.setRightContent(usage);//   commonSinglePickBean.getId()
                            }
                        })
                        .setContext(this);
                builder.setData(BasicInitDataUtils.getBuildingUseList(this))//住房用途(0:自住、1：出租)getBuildingUseList
                        .setFlag(0)
                        .setTitle(getString(R.string.Label_house_use));
                builder.build().showPickerView();

                break;
            case R.id.btn_commit:
                submit();
                break;
        }
    }

    @Override
    public void afterTextChanged(Editable s, int flag) {
        switch (flag) {
            case 1:
                estateName = s.toString().trim();
                break;
            case 2:
                estateAdress = s.toString().trim();
                break;
            case 3:
                houseNum = s.toString().trim();
                break;
            case 4:
                roomNum = s.toString().trim();
                break;
            case 5:
                area = s.toString().trim();
                break;
            case 6:
                developerName = s.toString().trim();
                break;
        }
    }
}
